<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	var $errors = '';
	 
	 
	public function index()
	{
		$this->load->view('index');
		//$this->student_info();
	}
	
	public function processAccounts(){
		$this->load->helper('form');
		$size = $this->input->post('h');
		$size2 = $this->input->post('h');
		$this->load->model('admin_model');

		for($i=1;$i<=$size;$i++){
			$s = 'disapproved'.$i;
			if($this->input->post($s)!=NULL){
				$this->admin_model->disapprove($this->input->post($s));
			};
		}

		for($i=1;$i<=$size;$i++){
			$s = 'approved'.$i;
			if($this->input->post($s)!=NULL){
				$data = $this->input->post($s);
				$this->admin_model->addAccount($data);
			}
		}

	}

	public function processInstructor(){
		$this->load->helper('form');
		$size = $this->input->post('num');
		$this->load->model('admin_model');
		

		for($i=1;$i<=$size;$i++){
			$s = 'iapproved'.$i;
			if($this->input->post($s)!=NULL){
				$data = $this->input->post($s);
				$this->admin_model->addInstructorAccount($data);
			}
		}

		//after approving or disapproving accounts,
		//redirect to the viewing page
	}

	public function processLogin()
	{
		$this->form_validation->set_rules('username','username','trim|required|valid_email'); 
		$this->form_validation->set_rules('password','password','trim|required|min_length[5]|max_length[20]'); 


		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('index');
		}
		else
		{
			$email = $this->input->post('username');
			$password = $this->input->post('password');

			$data = array('email' => $email,
					      'password' =>$password);			
			$this->load->model('user_model');
			
			$row = $this->user_model->login_user($data);
			if ($row)
			{
			   $this->session->set_userdata('user', $row->username);
			   if($row->account_type=="encoder") { redirect('home/encoder', ''); }
			   else if($row->account_type=="admin") { 
			   
			   	redirect('home/admin', ''); 
			   }
			   else if($row->account_type=="student") {
			   	$umd5 = $row->username_md5;

			   	$login_lock = $this->user_model->login_lock_student($umd5);
 			   	if(!$login_lock) redirect('home/student/'.$umd5.'', ''); 
			   	else echo "This account is currently logged in";

			   }
			}

			else $this->load->view('index');
		
		}

	}
	public function processSignup()
	{
		$this->load->model('user_model');
		
		
		switch($this->input->post('type')){
			case 'student': 
			$this->user_model->createStudentAccount();
			break;
			case 'faculty':
			$this->user_model->createFacultyAccount();
			break;
			case 'admin':
			$this->user_model->createEncoderAccount();
			break;

		}
//		$this->load->view('index');
	}

	public function logout(){
		$this->session->sess_destroy();
		redirect('home', '');
	}

	public function logout_student($id)
	
	{
		$this->load->model('user_model');
		$this->user_model->logout_unlock($id);
		$this->session->sess_destroy();
		redirect('home', '');
	}

	public function encoder()
	{
		$this->load->view('header_login_success');
		$this->load->view('encoder_pages/encoder_login_success');
	}

	public function encoder_view_students()
	{
		$this->load->view('header_login_success');
		$this->load->view('encoder_pages/encoder_login_success_body1');
		$this->load->model('user_model');
			
		/**main view for the data table**/
		$data["data"] = $this->user_model->view_all_students();
		$this->load->view('encoder_pages/encoder_view_students', $data);
		$this->load->view('encoder_pages/encoder_login_success_footer');
	}

	public function encoder_edit_student()
	{
		$this->load->view('header_login_success');
		$this->load->view('encoder_pages/encoder_login_success_body1');
		$this->load->model('user_model');
			
		/**main view for the data table**/
		$md5 = $this->input->get('std_no');
		$stdno = $this->user_model->get_stdno($md5);
		$data['data'] = $this->user_model->get_acad_info($stdno);
		$this->load->view('encoder_pages/encoder_edit_student', $data);
		$this->load->view('encoder_pages/encoder_login_success_footer');
	}
	
	public function admin()
	{
		$this->load->view('header_login_success');
		$this->load->view('admin_pages/admin_login_success');
	}

	public function admin_pending_process(){
		$this->load->view('header_login_success');
		$this->load->view('admin_pages/admin_login_success_body');
		$this->load->model('user_model');
		$data = array(
			'data_stud' => $this->user_model->get_approved_students(),
			'data_fact' => $this->user_model->get_approved_faculty()
		);
		$this->load->view('admin_pages/admin_pending_process',$data);		
		$this->load->view('admin_pages/admin_login_success_footer');
	}

	public function student($number)
	{
		$this->load->view('header_login_success');
		$this->student_info($number);
	}
	
	/*Student Controllers*/
	public function student_info($number){
		$this->load->model('student_model');
		
		$data['student'] = $this->student_model->view_info($number);
		
		$this->load->view('student_pages/student_basicinfo',$data);
	
	}
	
	public function student_info_editview($number){
		$this->load->model('student_model');
		
		$data['student'] = $this->student_model->view_info($number);
			
		$this->load->view('student_pages/student_basicinfo_edit',$data);
	}
	
	public function	student_advisers($type){
		$this->load->model('student_model');
		
		if($type == 'sp'){
			$data['type'] = 'sp';
			$data['adviser'] = $this->student_model->advisers($type);
		}else if($type == 'reg'){
			$data['type'] = 'reg';
			$data['adviser'] = $this->student_model->advisers($type);
		}else{
			$this->index();
		}
		
		$this->load->view('student_pages/student_adviser',$data);
	}
	

	public function student_edit_info($id,$md5){
		$this->load->helper(array('url','form'));
		$this->load->library(array('form_validation','session'));
		
		$errors = '';	//reset errors
		
		$userdata = array('student_no' => $id);
		$this->session->set_userdata($userdata);
		
		$this->form_validation->set_rules('MOBILE_NO','Mobile number','exact_length[11]|required');
		$this->form_validation->set_rules('HOME_ADDRESS','Home address','trim|required');
		$this->form_validation->set_rules('HOME_CONTACT','Home contact no.','trim|required');
		$this->form_validation->set_rules('COLLEGE_ADDRESS','College address','trim');
		$this->form_validation->set_rules('COLLEGE_CONTACT','College contact no','trim');
		$this->form_validation->set_rules('CURRICULUM_TAKEN','Curriculum taken', 'trim|alpha_dash');
		
		
		//call controller for uploading the files if user selected a file
		if ( $_FILES AND $_FILES['PHOTO']['name'] ){
			$upload_flag = $this->do_upload();
		}
		
		if($this->form_validation->run()==FALSE){
			$this->load->view('student_pages/student_basicinfo_edit');
		}else{
			$this->load->model('student_model');
			$a = $this->student_model->edit_basicinfo($id,$md5);
			
			if($a[0]){

				//return to view basic info page
 			   	redirect('home/student/'.$a[1].'', ''); 
			}else{
				//return to edit basic info page
				//with message of unsuccessful edit
			}
		}
	}
	
	public function student_acadhist(){
		$this->load->model('student_model');
		
		$data['student'] = $this->student_model->view_acadhist();
		
		$this->load->view('student_pages/student_acadhist',$data);
	}
	/*Student Controllers*/

	/*Upload Controller*/
	public function do_upload()
	{
		$config['upload_path'] = './user_images/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['file_name'] = $this->session->userdata('student_no').'.JPG';
		$config['max_size']	= '1000';
		$config['max_width']  = '1000';	
		$config['max_height']  = '1000';
		$config['overwrite'] = TRUE;

		$this->load->library('upload', $config);

		$upload=$this->input->post('PHOTO');
		
		if (!$this->upload->do_upload('PHOTO'))
		{
			$error = array('error' => $this->upload->display_errors());
			
			
			foreach($error as $e){
				$errors = $errors."<br/>".$e;
			}
			
			
			return false;
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());

			return true;
		}
	}
	/*Upload Controller*/
}



/* End of file home.php */
/* Location: ./application/controllers/home.php */